﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HomeworkFormatting.net.bing.api;

//http://api.bing.net/search.wsdl?AppID=C92824F2D31A2EFCFC3AEC31168E9DBC5AC2DA8D&Version=2.2

namespace StringFormatting
{
    public class HomeworkConversion
    {
        public static string[] formatHomework(string searchTerms)
        {
            // maximum number of search results
            uint maxSearchResults = 10;
            
            //set up the services and the remediation URLs array
            BingService bservice = new BingService();
            string[] remediationResultsArray = new string[maxSearchResults];
            int remediationResultsArrayLength = 0;

            // complete            
            SearchRequest request = new SearchRequest();

            request.AppId = "C92824F2D31A2EFCFC3AEC31168E9DBC5AC2DA8D";
            request.Query = searchTerms;
            request.Sources = new SourceType[] { SourceType.Web };

            request.Web = new WebRequest();
            request.Web.Count = maxSearchResults;
            request.Web.CountSpecified = true;

            SearchResponse response = bservice.Search(request);

            if (request.Query != "" && request.Query != null)
            {
                // convert the web search results into utls addresses
                for (int i = 0; i < maxSearchResults; i++)
                {
                    remediationResultsArray[remediationResultsArrayLength] = response.Web.Results[i].Url;
                    remediationResultsArrayLength++;
                }
            }
            else
            {
                remediationResultsArray[0] = "Search failed in part or whole.";
            }

            return remediationResultsArray;
        }
    }
}
